package com.googleplus.webservices;

import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.PostConstruct;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import com.googleplus.Person;
import com.googleplus.Post;
import com.googleplus.ejb.PlusServiceJSONImpl;
import com.googleplus.ejb.PlusService;

@Path("/person/{id}")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public class PersonResource {

	@PathParam("id")
	private String id;
	
	private PlusService svc;
	
	@Context
	private UriInfo uriInfo;
	
	private String personUrl;
	
	
	public PersonResource(){
	}

	@PostConstruct
	void init(){
		svc = new PlusServiceJSONImpl();
		personUrl = uriInfo.getBaseUriBuilder().path(PersonResource.class).build(id).toString();
	}
	
	@GET
	public PersonResult getPerson(){
		try {
			PersonResult p = new PersonResult(svc.getPerson(id));
			p.followersUrl = uriInfo.getBaseUriBuilder().path(PersonResource.class).path(PersonResource.class,"getFollowers").build(p.id).toString();
			p.followingUrl = uriInfo.getBaseUriBuilder().path(PersonResource.class).path(PersonResource.class,"getFollowing").build(p.id).toString();
			p.postsUrl = uriInfo.getBaseUriBuilder().path(PersonResource.class).path(PersonResource.class,"getPosts").build(p.id).toString();
			p.url = uriInfo.getRequestUri().toString();
			return p;
		} catch (Exception e) {
			throw new WebApplicationException(500);
		}
	}
	
	@GET
	@Path("/followers")
	public PeopleResult getFollowers(){
		try {
			ArrayList<Person> people = svc.getFollowers(id);
			PeopleResult result = new PeopleResult();
			result.url = uriInfo.getRequestUri().toString();
			result.personUrl = personUrl;
			result.people = peopleToRefs(people,"/follower");
			return result;
		} catch (Exception e) {
			throw new WebApplicationException(500);
		}
		
	}

	private ArrayList<PersonReference> peopleToRefs(ArrayList<Person> people, String rel) {
		ArrayList<PersonReference> result = new ArrayList<PersonReference>();
		for( Person p : people ){
			PersonReference ref = new PersonReference();
			ref.uri = uriInfo.getBaseUriBuilder().path(PersonResource.class).build(p.id).toString();
			ref.rel = rel;
			result.add(ref);
		}
		return result;
	}
	
	@GET
	@Path("/following")
	public PeopleResult  getFollowing(){
		try {
			ArrayList<Person> people = svc.getFollowing(id);
			PeopleResult result = new PeopleResult();
			result.url = uriInfo.getRequestUri().toString();
			result.personUrl = personUrl;
			result.people = peopleToRefs(people,"/following");
			return result;
		} catch (Exception e) {
			throw new WebApplicationException(500);
		}
		
	}
	
	@GET
	@Path("/posts")
	public PostsResult getPosts(){
		try {
			PostsResult result = new PostsResult();
			result.url = uriInfo.getRequestUri().toString();
			result.personUrl = personUrl;
			result.posts = svc.getPosts(id);
			return result;
		} catch (Exception e) {
			throw new WebApplicationException(500);
		}
	}
	
}
